

var commuser;
apiready = function(){
    
    api.setRefreshHeaderInfo({
		visible: true,
		bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
	    textDown: '下拉刷新...',
	    textUp: '松开刷新...',
	    showTime: true
    },function(ret,err){
	    	loadData();
	    	api.refreshHeaderLoadDone();
    });

    //监听获取用户信息
	api.addEventListener({
	    name: 'refreshuser'
	}, function(ret){
	    if(ret){
	      loadData();
	    }
	});
     
     
    loadData();
};


//加载数据
function loadData(){
    
    commuser=$api.getStorage('member2');
    api.showProgress({
        title: '加载中...',
        modal: false
    });
    
    
    $api.attr($api.byId('headimg'),'src',commuser.headimgurl?serviceUrl+commuser.headimgurl:'images/user/defaultphoto.png');
    $api.text($api.byId('name'), commuser.nickName||commuser.account);
    $api.text($api.byId('status'), commuser.status);
    
    
    $api.text($api.byId('grade'), commuser.grade);
    $api.text($api.byId('money'), commuser.money.toFixed(2));
    $api.text($api.byId('points'), commuser.points);
    
    
     api.hideProgress();    
     
};




//修改头像
function duFileUpLoad(){
    
/*示例简介:
 弹出一个选择框,用户可以选择图片来源;
 根据用户选择打开图库或者相机;
 */

	api.confirm({
	    title: "提示",
	    msg: "您想要从哪里选取图片 ?",
	    buttons:["优雅自拍", "相册收藏","取消"]
	},function(ret,err){
	
	    if(3 == ret.buttonIndex){ // 用户取消.
	        return;
	    }
	
	    var sourceType = "album";
	
	    if(1 == ret.buttonIndex){ /* 打开相机*/
	        sourceType = "camera";
	    }
	
	    api.getPicture({
	        sourceType: sourceType,
	        encodingType: "jpg",
	        mediaValue: "pic"
	    }, function(ret, err){
	        if (ret.data) {
	        
	           // alert(ret.data);
	            
	        	ajaxFileUpload(ret.data);
	        }
	    });
	});
    
    
};


//收货地址
function openaddress(){
    api.openWin({
	        name: 'address',
	        url: '../shop/address.html'
	 });
};


/**
 * 上传文件 
 */
function ajaxFileUpload(data){


	var connectionType = api.connectionType;  //比如: wifi
	if(connectionType=='none'){toast('当前无网络!');return;}
	
	 api.showProgress({
        title: '正在上传...',
        text: '请稍等'
    });
    
	api.ajax({
        url:serviceUrl+'/member/uploadimg',
        method: 'post',
        timeout: 30,
        dataType: 'json',
        returnAll: false,
        //report: true, //（可选项）是否实时返回上传文件进度
        data: {
            values: { name:"fileToUpload"},
            files: {fileToUpload: data }
        }
    },function(ret, err){
    		 if (ret) {
                if ($api.trim(ret.error) != '') {
                    toast(ret.error);
                }else {
                	//toast('上传成功!');
                	$api.attr($api.byId('headimg'),'src',serviceUrl+ret.filename);
                	
                	//去修改
                	 var data={
			             'memberID':commuser.memberID,
			             'headimgurl':ret.filename,
			         };
			       
			        do_ajax("/app/commuser/update",'post',data,function(result,err){
						api.hideProgress();
						if(result.resultCode == '0') {
							    $api.setStorage('member2',result.member2);
							    //发送刷新用户信息事件
							    api.sendEvent({
								    name: 'refreshuser',
								});
							    toast('修改成功!');
					 		}else{
					 			 toast(result.datas);
					 		}
					 });
	       
                	
                	
                }
            } else {
                api.alert({
                    msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
                });
            }
            api.hideProgress();
    		
        });   
}


//推荐人
function setrecman(){
	var member=$api.getStorage('member2');
	
    api.prompt({
        title:"推荐人",
        msg:"请输入推荐人手机号码",
        text:member.recman,
	    buttons:['确定', '取消']
	},function(ret,err){
	    if(ret.buttonIndex == 1){

			if(ret.text){
				var data = {
					'memberID':member.memberID,
					'recman':ret.text
				}
				
				do_ajax("/app/community/updateuser",'post',data,function(result,err){
					api.hideProgress();
					if(result.resultCode == '0') {
						member.recman = ret.text;
						$api.setStorage('member2',member);
						alert('修改成功!');
					 }else{
					 	alert(result.datas);
					 }
				});
			}
			
	    }
	});
    
};


//退出
function logout(){
        api.showProgress({
	        title: '正在退出...',
	        modal: false
	    });
	    
	    $api.rmStorage('account');
	    $api.rmStorage('member');
	    $api.rmStorage('member2');
	    
	    //发送用户登陆退出监听事件
		api.sendEvent({
		    name: 'refreshUserinfo',
		});
		
		//更新物业服务的界面
		api.sendEvent({
		    name: 'refreshProperty',
		});
		
		api.execScript({
		    name: 'root',
		    script: 'randomSwitchBtn("","index",0);'
		});
	
		
        api.hideProgress();
};

